# Import the datetime class from the datetime module to work with date and time objects.
from datetime import datetime

# Import the pytz library for handling timezones.
import pytz

# Define a constant for the Unix epoch (January 1, 1970, at 00:00:00 UTC).
# This is used as a reference point for calculating timestamps.
EPOCH = datetime(1970, 1, 1)


def datetime_to_epoch(dttm):
    """
    Converts a datetime object to a Unix epoch timestamp in milliseconds.

    :param dttm: The datetime object to convert.
    :return: The number of milliseconds since the Unix epoch.
    """
    # Check if the datetime object is timezone-aware.
    if dttm.tzinfo:
        # If it has timezone info, convert it to UTC to ensure a consistent epoch calculation.
        dttm = dttm.replace(tzinfo=pytz.utc)
        # Create a timezone-aware epoch datetime object.
        epoch_with_tz = pytz.utc.localize(EPOCH)
        # Calculate the difference in seconds and convert to milliseconds.
        return (dttm - epoch_with_tz).total_seconds() * 1000
    # If the datetime object is naive (no timezone info), assume it's in UTC.
    return (dttm - EPOCH).total_seconds() * 1000


def now_as_float():
    """
    Gets the current UTC time as a floating-point number representing the
    number of milliseconds since the Unix epoch.

    :return: The current time as a float (milliseconds since epoch).
    """
    # Get the current time in UTC and convert it to an epoch timestamp.
    return datetime_to_epoch(datetime.utcnow())
